from tortoise import fields
from tortoise.models import Model
from tortoise.validators import MinLengthValidator, MaxLengthValidator

class Student(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32,description="姓名")
    pwd=fields.CharField(max_length=32,min_length=8,description="密码")
    sno = fields.CharField(
        max_length=11,
        min_length=11,  # 设置最小长度
        description='学号',
        validators=[MinLengthValidator(11), MaxLengthValidator(11)],
        unique=True
    )
    avatar_blob = fields.BinaryField(
        description="学生头像"
    )
    email = fields.CharField(max_length=40, description='邮箱')
    cla_id=fields.ForeignKeyField("models.Class",related_name="students",null=True)

class Class(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32,description='班级编号')

    curse_id=fields.ManyToManyField("models.Course",related_name="classes")

class Teacher(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32,description='姓名')
    pwd = fields.CharField(max_length=32,min_length=8, description="密码")
    num = fields.CharField(
        max_length=11,
        min_length=11,
        description='编号',
        validators=[MinLengthValidator(11), MaxLengthValidator(11)],
        unique=True
    )
    email = fields.CharField(max_length=40,description='邮箱')
    avatar_blob = fields.BinaryField(
        description="教师头像"
    )
    fields.ForeignKeyField("models.Course",related_name="teachers")

class Course(Model):
    id = fields.IntField(pk=True)
    name=fields.CharField(max_length=128,description='课程名')

class Grade(Model):
    id = fields.IntField(pk=True)

    stu_id=fields.ForeignKeyField("models.Student",related_name="grades")
    course_id=fields.ForeignKeyField("models.Course",related_name="grades")

class User(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32,description="姓名")
    num=fields.CharField(
        max_length=11,
        min_length=11,
        validators=[MinLengthValidator(11), MaxLengthValidator(11)],
        unique=True,
        description='账号'
    )
    sf = fields.CharField(
        max_length=10,
        choices=[
            ("student", "学生"),
            ("teacher", "教师")
        ],
        default="student"
    )

class Admin(Model):
    id = fields.IntField(pk=True)
    num = fields.CharField(
        max_length=15,
        min_length=15,
        validators=[MinLengthValidator(15), MaxLengthValidator(15)],
        unique=True,
        description='账号'
    )
    pwd = fields.CharField(
        max_length=15,
        min_length=15,
        validators=[MinLengthValidator(15), MaxLengthValidator(15)],
        description='密码'
    )

